/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sindarin.Common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Vector;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

/**
 *
 * @author user
 */
public class CodeFileClass {

    public enum DataStructFile { FILEHEADER, SYMBOLTABLE, INSTVECTOR };

    public static FileHeader getHeaderFile(File f) throws Exception {
        FileHeader header;

        FileInputStream fIn = new FileInputStream(f);
        GZIPInputStream gIn = new GZIPInputStream(fIn);
        ObjectInputStream oIn = new ObjectInputStream(gIn);

        header = (FileHeader) oIn.readObject();

        oIn.close();
        gIn.close();
        fIn.close();

        return header;
    }

    public static HashMap loadFile(File f, String pass) throws Exception {
        HashMap ret = new HashMap();
        FileHeader header;

        FileInputStream fIn = new FileInputStream(f);
        GZIPInputStream gIn = new GZIPInputStream(fIn);
        ObjectInputStream oIn = new ObjectInputStream(gIn);
        GZIPInputStream gIn2 = null;

        CipherInputStream cIn = null;

        header = (FileHeader) oIn.readObject();
        ret.put(DataStructFile.FILEHEADER, header);

        if (header.cypher) {
            cIn = new CipherInputStream(gIn, SimpleCipher.getCipher(pass, Cipher.DECRYPT_MODE));
            gIn2 = new GZIPInputStream(cIn);
            oIn = new ObjectInputStream(gIn2);
        }

        ret.put(DataStructFile.INSTVECTOR, (Vector<Instruction>) oIn.readObject());
        ret.put(DataStructFile.SYMBOLTABLE, (SymbolTable) oIn.readObject());

        oIn.close();
        if (header.cypher) {
            gIn2.close();
            cIn.close();
        }
        gIn.close();
        fIn.close();

        return ret;
    }

    public static void saveFile(File f, String pass, FileHeader header, Vector<Instruction> instV, SymbolTable sTable) throws Exception {
        FileOutputStream fOut = new FileOutputStream(f);
        GZIPOutputStream gOut = new GZIPOutputStream(fOut);
        ObjectOutputStream oOut = new ObjectOutputStream(gOut);
        GZIPOutputStream gOut2 = null;

        CipherOutputStream cOut = null;

        oOut.writeObject(header);

        if (header.cypher) {
            cOut = new CipherOutputStream(gOut, SimpleCipher.getCipher(pass, Cipher.ENCRYPT_MODE));
            gOut2 = new GZIPOutputStream(cOut);
            oOut = new ObjectOutputStream(gOut2);
        }

        oOut.writeObject(instV);
        oOut.writeObject(sTable);

        oOut.close();
        if (header.cypher) {
            gOut2.close();
            cOut.close();
        }
        gOut.close();
        fOut.close();
    }

    public static String getPassDialog()
    {
        JPasswordField passField = new JPasswordField();
        JPasswordField repPassField = new JPasswordField();

        do {
            //switch (javax.swing.JOptionPane.showConfirmDialog(null, new Object[]{bundle.getString("passMsg"), passField, bundle.getString("repPassMsg"), repPassField}, bundle.getString("passMsgTittle"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE)) {
            switch (javax.swing.JOptionPane.showConfirmDialog(null, new Object[]{"passMsg", passField, "repPassMsg", repPassField}, "passMsgTittle", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE)) {
                case (JOptionPane.OK_OPTION):
                    if (!new String(passField.getPassword()).equals(new String(repPassField.getPassword()))) {
                        //javax.swing.JOptionPane.showMessageDialog(null, bundle.getString("errMsgPassNotEqual.text"));
                        javax.swing.JOptionPane.showMessageDialog(null, "errMsgPassNotEqual.text");
                        passField.setText("");
                        repPassField.setText("");
                        break;
                    }
                    if (passField.getPassword().length <= 0) {
                        //javax.swing.JOptionPane.showMessageDialog(null, bundle.getString("errMsgPassEmpty.text"));
                        javax.swing.JOptionPane.showMessageDialog(null, "errMsgPassEmpty.text");
                        passField.setText("");
                        repPassField.setText("");
                        break;
                    }
                    return new String(passField.getPassword());
                case (JOptionPane.CANCEL_OPTION):
                    return null;
            }
        } while (true);
    }
}
